package com.process.two;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

public class Producer {

    public static void main(String[] args) throws Exception {
        // 映射共享内存文件
        FileChannel channel = FileChannel.open(
                java.nio.file.Paths.get("D:\\main.dat"),
                StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024);

        Scanner scanner = new Scanner(System.in);
        while (true) {
            // 从标准输入读取一行数据
            String line = scanner.nextLine();

            // 写入共享内存
            buffer.put(line.getBytes());

            // 如果输入的是 exit，退出循环
            if ("exit".equals(line)) {
                break;
            }
        }

        channel.close();
    }
}

